package com.qf.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.student.bo.CheckTimeBo;
import com.qf.student.dao.CourseMapper;
import com.qf.student.dao.StudentCourseMapper;
import com.qf.student.dao.StudentMapper;
import com.qf.student.dao.TeacherMapper;
import com.qf.student.entity.Course;
import com.qf.student.entity.Student;
import com.qf.student.entity.StudentCourse;
import com.qf.student.entity.Teacher;
import com.qf.student.response.R;
import com.qf.student.service.IStudentCourseService;
import com.qf.student.vo.input.UpdateScoreInputVo;
import com.qf.student.vo.output.StudentCourseOutputVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-11-21
 */
@Service
@Transactional
public class StudentCourseServiceImpl extends ServiceImpl<StudentCourseMapper, StudentCourse> implements IStudentCourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private StudentCourseMapper studentCourseMapper;


    @Override
    public R selectCourse(Integer courseId, Integer userId) {
        //1、判断课程是否存在
        Course course = courseMapper.selectById(courseId);
        if (course == null) {
            return R.createFAIL("该课程不存在，请重新选择");
        }
        //2、判断学员是否存在
        Integer studentGrade = studentMapper.getStudentGrade(userId);
        if (studentGrade == null) {
            return R.createFAIL("该学员不存在，请重新选择");
        }

        //3、判断学员与课程是否是统一年级的
        if (!course.getCourseGrade().equals(studentGrade)) {
            return R.createFAIL("该学员无法选修其它年级的课程");
        }
        //4、判断该课程学员是否已经选修
        StudentCourse studentCourse = studentCourseMapper.selectOne(
                new QueryWrapper<StudentCourse>()
                        .eq("sc_student_id", userId)
                        .eq("sc_course_id", courseId)
        );
        if (studentCourse != null) {
            return R.createFAIL("该学员已经选修了这门课程");
        }

        //5、判断课程人数是否已满
        if (course.getCourseMaxSize() <= course.getCourseSelectedCount()) {
            return R.createFAIL("该课程选修人数已满");
        }
        //6、判断课程时间是否重复
        List<String> timeList = courseMapper.getStudentTimeList(userId);
        for (String time : timeList) {
            if (CheckTimeBo.timeConflict(time, course.getCourseTime())) {
                return R.createFAIL("该课程时间冲突，无法选择该课程");
            }
        }

        //7、新增数据
        StudentCourse sc = new StudentCourse();
        sc.setScCourseId(courseId);
        sc.setScStudentId(userId);
        int result = studentCourseMapper.insert(sc);
        if (result > 0) {
            //8、修改课程数据 人数+1
            course.setCourseSelectedCount(course.getCourseSelectedCount() + 1);
            if (courseMapper.updateById(course) > 0) {
                return R.createSuccess("课程选修成功");
            }
        }

        return R.createFAIL("课程选修失败，请稍候再试");
    }

    @Override
    public R getStudentCourse(Integer userId) {
        Student student = studentMapper.selectById(userId);
        if (student == null) {
            return R.createFAIL("学员信息有误");
        }
        List<StudentCourseOutputVo> studentCourses = studentCourseMapper.getStudentCourse(userId);
        return R.createSuccess(studentCourses);
    }

    @Override
    public R unselectCourse(Integer userId, Integer courseId) {
        StudentCourse studentCourse = studentCourseMapper.selectOne(
                new QueryWrapper<StudentCourse>()
                        .eq("sc_student_id", userId)
                        .eq("sc_course_id", courseId)
        );

        if (studentCourse == null) {
            return R.createFAIL("未选择该课程，退选失败");
        }

        Course course = courseMapper.selectById(courseId);
        if (course == null) {
            return R.createFAIL("课程不存在，退选失败");
        }

        course.setCourseSelectedCount(course.getCourseSelectedCount() - 1);
        if (courseMapper.updateById(course) > 0) {
            if (studentCourseMapper.deleteById(studentCourse.getScId()) > 0) {
                return R.createSuccess("退选成功");
            }
        }

        return R.createFAIL("退选失败，请稍后再试");
    }

    @Override
    public R getStudentScore(Integer userId) {
        Student student = studentMapper.selectById(userId);
        if (student == null) {
            return R.createFAIL("学员信息有误");
        }
        List<StudentCourseOutputVo> studentCourses = studentCourseMapper.getStudentScore(userId);
        return R.createSuccess(studentCourses);
    }

    @Override
    public R teacherUpdateScore(UpdateScoreInputVo updateScoreInputVo, Integer userId) {
        Teacher teacher = teacherMapper.selectById(userId);
        if (teacher == null) {
            return R.createFAIL("教师信息有误");
        }

        StudentCourse studentCourse = new StudentCourse();
        BeanUtils.copyProperties(updateScoreInputVo, studentCourse);

        if (studentCourseMapper.updateById(studentCourse) > 0) {
            return R.createSuccess("成绩录入成功");
        }
        return R.createFAIL("成绩录入失败");
    }

}
